﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using GameSPK.Helpers;

namespace GameSPK.DAO
{
    public class CuocThiDAO
    {
        public static bool setCuocThiStart(General cuocthi)
        {
            bool result = true;
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            try
            {
                var gene = (from ge in db.Generals
                           where ge.MaCuocChoi == cuocthi.MaCuocChoi
                           select ge).Single();
                gene.isStart = true;
                db.SubmitChanges();
            }
            catch
            {
                result = false;
            }
            return result;
        }
        public static bool setCuocThi(General cuocthimoi)
        {
            bool result = true;
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            try
            {
                db.Generals.InsertOnSubmit(cuocthimoi);
                db.SubmitChanges();
            }
            catch
            {
                result = false;
            }
            return result;
        }
        public static bool updateCuocThi(ObservableCollection<ICuocChoi> taphop)
        {
            bool result = true;
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            try
            {
                var general = (from ge in db.Generals
                              where ge.isDelete == false
                                    && ge.Chon == true
                              select ge).Single();
                //general.Chon = false;
                //db.SubmitChanges();
                foreach (ICuocChoi ch in taphop)
                {
                    if (ch.IsEnabled == true)
                    {
                        if (ch.macuocchoi != general.MaCuocChoi)
                        {
                            general.Chon = false;
                            var chongeneral = (from ge in db.Generals
                                               where ge.isDelete == false
                                                     && ge.MaCuocChoi == ch.macuocchoi
                                               select ge).Single();
                            chongeneral.Chon = true;
                            
                        }
                        break;
                    }
                }
                foreach (ICuocChoi ch in taphop)
                {
                    var chongeneral = (from ge in db.Generals
                                       where ge.isDelete == false
                                             && ge.MaCuocChoi == ch.macuocchoi
                                       select ge).Single();
                    chongeneral.GhiChu = ch.Note;
                    chongeneral.NgayBatDau = ch.NgayThi;
                    chongeneral.isStart = ch.IsStart;
                    if (chongeneral.isStart == false)
                    {
                        RollBackDataCuocThi(chongeneral);
                    }
                }
                db.SubmitChanges();
            }
            catch
            {
                result = false;
            }
            return result;
        }
        public static bool RollBackDataCuocThi(General cuocthi)
        {
            bool result = true;
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            try
            {
                var ch_ch = from ch in db.CauHoi_CuocChois
                            where ch.MaCuocChoi == cuocthi.MaCuocChoi
                            select ch;
                foreach (CauHoi_CuocChoi cauhoi_cuocchoi in ch_ch)
                {
                    cauhoi_cuocchoi.Chon = false;
                }
                db.SubmitChanges();
            }
            catch
            {
                result = false;
            }
            return result;
        }
        public static General getCuocThiRoot()
        {
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            try
            {
                var general = (from ge in db.Generals
                               where ge.isRoot == true && ge.isDelete==false
                               select ge).Single();

                return general;
            }
            catch
            {
                return null;
            }
        }
        public static ObservableCollection<ICuocChoi>  getAllCuocThi()
        {
            ObservableCollection<ICuocChoi> taphop;
            taphop = new ObservableCollection<ICuocChoi>();
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            try
            {
                var general = (from ge in db.Generals
                              where ge.isDelete==false
                               select ge);

                foreach (General ge in general)
                {
                    ICuocChoi cuocchoi = new dataCuocChoi(ge);
                    taphop.Add(cuocchoi);
                }
                return taphop;
            }
            catch
            {
                return taphop;
            }
        }
        //public static ObservableCollection<ICuocChoi> getAllCuocThi()
        //{
        //    ObservableCollection<ICuocChoi> taphop;
        //    taphop = new ObservableCollection<ICuocChoi>();
        //    DBTroChoiDataContext db = new DBTroChoiDataContext();
        //    try
        //    {
        //        var general = (from ge in db.Generals

        //                       select ge);

        //        foreach (General ge in general)
        //        {
        //            ICuocChoi cuocchoi = new dataCuocChoi(ge);
        //            taphop.Add(cuocchoi);
        //        }
        //        return taphop;
        //    }
        //    catch
        //    {
        //        return taphop;
        //    }
            
        //}

        public static ObservableCollection<ICuocChoi> getNoteCuocThi(string note)
        {
            ObservableCollection<ICuocChoi> taphop;
            taphop = new ObservableCollection<ICuocChoi>();
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            try
            {
                var general = (from ge in db.Generals
                               where ge.GhiChu.Contains(note) && ge.isDelete == false
                               select ge);

                foreach (General ge in general)
                {
                    ICuocChoi cuocchoi = new dataCuocChoi(ge);
                    taphop.Add(cuocchoi);
                }
                return taphop;
            }
            catch
            {
                return taphop;
            }

        }

        public static ObservableCollection<ICuocChoi> getDateCuocThi(DateTime? dtfrom, DateTime? dtto)
        {
            ObservableCollection<ICuocChoi> taphop;
            taphop = new ObservableCollection<ICuocChoi>();
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            try
            {
                var general = (from ge in db.Generals
                               where ge.NgayBatDau >= dtfrom && ge.NgayBatDau <= dtto
                                        && ge.isDelete == false
                               select ge);

                foreach (General ge in general)
                {
                    ICuocChoi cuocchoi = new dataCuocChoi(ge);
                    taphop.Add(cuocchoi);
                }
                return taphop;
            }
            catch
            {
                return taphop;
            }

        }
        public static General getCuocThiChon()
        {
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            try
            {
                var general = (from ge in db.Generals
                               where ge.Chon == true
                               && ge.isDelete == false
                               select ge).Single();

                return general;
            }
            catch
            {
                return null;
            }
        }

    }
}
